<template>
	<view class="all">
		<img style="height: 40vh;width: 40vw;" :src="img" alt="" />
		<view class="confirm" @click="confirm">
			确认
			<img data-v-7dc53fda="" src="/static/right.png" alt="" class="item_img">


		</view>
		<view class="tocamera" @click="toback">
			重新拍摄
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				img: null,
				img1: null,
				img2: null
			}
		},
		onLoad() {
			this.img1 = uni.getStorageSync('img') || null
			this.img2 = uni.getStorageSync('img2') || null
			if (this.img1 && this.img2) {
				console.log('img2');
				this.img = this.img2
			} else {
				console.log('img1');
				this.img = this.img1
			}
			console.log(this.img);
		},
		methods: {
			confirm() {
				let url = [];
				let that = this
				if (this.img1 && this.img2) {
					wx.uploadFile({
						url: '/api/common/upload',
						filePath: this.img1,
						name: 'file',
						success: res => {
							console.log(res);
							let data1 = JSON.parse(res.data)
							console.log(data1);
							url.push(data1.data.url)
						},
						fail: err => {
							console.log(err);
							wx.showToast({
								icon: 'error',
								title: err.data.msg
							})
							return false
						},
						complete: () => {
							wx.uploadFile({
								url: '/api/common/upload',
								filePath: that.img2,
								name: 'file',
								success: res => {
									console.log(res);
									let data = JSON.parse(res.data)
									url.push(data.data.url)
									let str = url.join(',')
									let email = uni.getStorageSync('mail')
									console.log(url, str);
									wx.request({
										url: '/api/index/push_image',
										method: 'POST',
										data: {
											images: str,
											email: email
										},
										success: res => {
											console.log(res);
											let store_id = uni.getStorageSync('store')
												.data || 0
											console.log(store_id);
											let webside = uni.getStorageSync('webside')
											let data = {
												store_id,
												webside
											}
											wx.request({
												url: '/api/index/shoppinginfo',
												method: 'POST',
												data: data,
												success: res => {
													console.log(res);
												}
											})
										},
										fail: err => {
											console.log(err);
										}
									})
								},
								fail: err => {
									console.log(err);
									wx.showToast({
										icon: 'error',
										title: err.data.msg
									})
									return false
								}
							})
						}
					})


					uni.removeStorageSync('img');
					uni.removeStorageSync('img2');
					uni.navigateTo({
						url: '/pages/mail/finish'
					})
				} else {
					uni.navigateTo({
						url: '/pages/mail/camera'
					})
				}

			},
			toback() {
				if (this.img1 && this.img2) {
					uni.removeStorageSync('img2')
					uni.navigateTo({
						url: '/pages/mail/camera'
					})
				} else {
					uni.removeStorageSync('img')
					uni.navigateTo({
						url: '/pages/mail/camera'
					})
				}

			}
		}
	}
</script>

<style>
	.confirm {
		margin-top: 20px;
		background-color: #fff;
		width: 100%;
		height: 50px;
		display: flex;
		justify-content: center;
		align-items: center;
		font-weight: bold;
		font-size: 15px;
	}

	.all {
		background-color: #000;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 0 5%;
		height: 100vh;
		width: 90%;
	}

	.item_img {
		width: 19px;
		height: 19px;
		color: #000;
		padding-left: 10px;
	}

	.tocamera {
		margin-top: 40px;
		color: #fff;
		font-size: 12px;
		text-decoration: underline;
	}
</style>